<template>
  <div class="app-container">
    <div>
      <el-form ref="form" :model="queryParams" label-width="80px" inline>
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="queryParams.name" size="small"/>
        </el-form-item>
        <el-form-item label="角色标识" prop="roleValue">
          <el-input v-model="queryParams.roleValue" size="small"/>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="addFormVisible = true" size="small">添加</el-button>
          <el-button type="primary" @click="getList" size="small">查询</el-button>
          <el-button type="primary" @click="resetForm('form')" size="small">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div>
      <el-table v-loading="tableLoading"
                :data="tableData"
                height="600"
                border>
        <el-table-column type="selection"></el-table-column>
        <el-table-column label="" prop="id"></el-table-column>
        <el-table-column label="创建时间" prop="createTime"></el-table-column>
        <el-table-column label="修改时间" prop="updateTime"></el-table-column>
        <el-table-column label="角色名称" prop="name"></el-table-column>
        <el-table-column label="角色权限标识" prop="roleValue"></el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
              <el-button size="small" type="text" @click="()=>{viewVisible=true;viewData = scope.row}">查看</el-button>
              <el-button size="small" type="text" @click="setEditData(scope.row)">编辑</el-button>
            <el-button size="small" type="text" @click="loadRolePermissions(scope.row.id)">资源管理</el-button>
            <br>
            <el-popconfirm
                confirm-button-text='好的'
                cancel-button-text='不用了'
                icon="el-icon-info"
                icon-color="red"
                @confirm="remove(scope.row)"
                title="确定删除吗？"
              >
                <el-button size="small" slot="reference" type="text">删除</el-button>
              </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        background
        :page-sizes="pageWrapper.pageSizes"
        :total="pageWrapper.total"
        :limit.sync="pageWrapper.size"
        :page.sync="pageWrapper.current"
        auto-scroll
        @pagination="getList"
        layout="sizes,prev, pager, next"
      />
    </div>

    <!-- form  -->
    <el-dialog title="添加" :visible.sync="addFormVisible"
               @close="()=>{this.resetForm('addForm')}">
      <el-form ref="addForm" label-width="80" :rules="roleRules" :model="addFormData">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="addFormData.name" size="small"/>
        </el-form-item>
        <el-form-item label="角色权限标识" prop="roleValue">
          <el-input v-model="addFormData.roleValue" size="small"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="createTRole('addForm')">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改" :visible.sync="editFormVisible"
               @close="()=>{this.resetForm('editForm')}">
      <el-form ref="editForm" label-width="80" :rules="roleRules" :model="editFormData">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="editFormData.name" size="small"/>
        </el-form-item>
        <el-form-item label="角色权限标识" prop="roleValue">
          <el-input v-model="editFormData.roleValue" size="small"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateTRole('editForm')">确 定</el-button>
      </div>
    </el-dialog>


    <!--view-->
    <el-dialog :visible.sync="viewVisible" footer="" >
      <el-descriptions border title="查看">
        <el-descriptions-item label="">{{ viewData.id }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ viewData.createTime }}</el-descriptions-item>
        <el-descriptions-item label="修改时间">{{ viewData.updateTime }}</el-descriptions-item>
        <el-descriptions-item label="角色名称">{{ viewData.name }}</el-descriptions-item>
        <el-descriptions-item label="角色权限标识">{{ viewData.roleValue }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
    <!--资源编辑-->
    <el-dialog :visible.sync="editPermissionObject.permissionVisible" width="30%" >
      <el-tree
        ref="inner-treeBox"
        :data="editPermissionObject.allPermissions"
        show-checkbox
        :check-strictly="editPermissionObject.checkStrictly"
        check-on-click-node
        default-expand-all
        node-key="id"
        @check="changeCheckPermission"
        :props="{
        label:'name',
        children:'children',
      }">
      </el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateRolePermissions">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import {allPermission} from '@/api/system/tPermission'
import {pageData, create, update, removeById ,updateRolePermission ,loadRolePermissions } from "@/api/system/tRole";
import {pageMixin, formMixin} from "@/mixins/mixins";


export default {
  name: "TRole",
  mixins: [pageMixin, formMixin,],
  components: {Pagination,},
  data() {
    return {
      tableLoading: false,
      tableData: [],
      queryParams: {
        name: '',
        roleValue: '',
      },
      editPermissionObject:{
        checkStrictly:false,
        allPermissions:[],
        permissionVisible:false,
        checkRoleId:null,
        permissions:[],
      },
      addFormVisible: false,
      editFormVisible: false,
      viewVisible: false,
      viewData: {
        id: '',
        createTime: '',
        updateTime: '',
        name: '',
        roleValue: '',
      },
      addFormData: {
        name: '',
        roleValue: '',
      },
      editFormData: {
        name: '',
        roleValue: '',
      },
      roleRules: {
        name: [{required: true}],
        roleValue: [{required: true}],
      },
    }
  },
  mounted() {
    this.getList();
  },
  methods: {
    allPermission,
    getList() {
      this.tableLoading = true;
      pageData({current: this.pageWrapper.current, size: this.pageWrapper.size, ...this.queryParams}).then(res => {
        this.setPageWrapper(res.data);
        this.tableData = res.data.records;
        this.tableLoading = false;
      })
    },
    setEditData(row) {
      Object.keys(this.editFormData).forEach(key => {
        this.editFormData[key] = row[key]
      })
      this.editFormVisible = true;
    },
    updateTRole(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          update(this.editFormData).then(res => {
            this.$notify['success']({
              title: res.message
            })
            this.getList();
            this.editFormVisible = false;
          })
        }
      });

    },
    createTRole(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          create(this.addFormData).then(res => {
            this.$notify['success']({
              title: res.message
            })
            this.getList();
            this.addFormVisible = false;
          })
        }
      });

    },
    remove(row) {
      removeById(row.id).then(res => {
        this.$notify['success']({
          title: res.message
        })
        this.getList();
      })

    },
    updateRolePermissions(){
      if(this.editPermissionObject.checkRoleId){
        updateRolePermission({
          permissions:this.editPermissionObject.permissions,
          roleId:this.editPermissionObject.checkRoleId
        }).then((res) =>{
          this.editPermissionObject.permissionVisible = false
          this.$notify({
            type:'success',
            message:res.message
          })
        })
      }
    },
    loadRolePermissions(id){
      this.editPermissionObject.permissions = []
      this.editPermissionObject.checkRoleId = id
      this.editPermissionObject.checkStrictly = true;
      allPermission({status:1}).then(res=>{
        this.editPermissionObject.allPermissions = res.data;
        loadRolePermissions(this.editPermissionObject.checkRoleId).then(res =>{
          this.editPermissionObject.permissions = res.data.map(p =>p.permissionId);
          this.editPermissionObject.permissionVisible = true
          this.$nextTick(()=>{
            const ref = this.$refs['inner-treeBox'];
            ref.setCheckedKeys(this.editPermissionObject.permissions)
            this.editPermissionObject.checkStrictly = false;

          })
        })
      })


    },
    changeCheckPermission(){
      const ref = this.$refs['inner-treeBox'];
      const data = ref.getHalfCheckedKeys().concat(ref.getCheckedKeys());
      this.editPermissionObject.permissions = data;
    }


  }
}
</script>

<style scoped>

</style>
